package com.bl.student.service.biz.studio.impl;

import com.bl.student.common.enums.ApplyType;
import com.bl.student.common.utils.UUIDUtils;
import com.bl.student.dao.model.*;
import com.bl.student.dao.repository.ArtExamRespository;
import com.bl.student.dao.repository.InformationRepository;
import com.bl.student.dao.repository.StudioRespository;
import com.bl.student.dao.repository.UserRepository;
import com.bl.student.service.biz.studio.StudioService;
import com.bl.student.service.vo.DelBody;
import com.bl.student.service.vo.studio.StudioIsIn;
import com.bl.student.service.vo.studio.StudioListVo;
import com.bl.student.service.vo.studio.StudioVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liangweikang on 18/3/10.
 */
@Service
@Log4j2
public class StudioSerivceImpl implements StudioService {

    @Autowired
    StudioRespository studioRespository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    ArtExamRespository artExamRespository;

    @Autowired
    InformationRepository informationRepository;

    @Override
    public void del(String userid, DelBody id) {
        List<StudioEntity> studioEntities = studioRespository.findByUserIdAndBizId(userid,id.getId());

        if (studioEntities != null) {
            studioRespository.delete(studioEntities.get(0).getId());
        }
    }

    @Override
    public void add(String userid, DelBody id) {
        StudioEntity studioEntity = new StudioEntity();
        studioEntity.setCreateTime(new Date());
        studioEntity.setId(UUIDUtils.getUUID());
        studioEntity.setUserId(userid);
        studioEntity.setUpdateTime(new Date());
        studioEntity.setBizId(id.getId());
        studioEntity.setType(ApplyType.STUDIO);
        studioRespository.save(studioEntity);
    }

    @Override
    public void addPlan(String userid, DelBody id) {
        StudioEntity studioEntity = new StudioEntity();
        studioEntity.setCreateTime(new Date());
        studioEntity.setId(UUIDUtils.getUUID());
        studioEntity.setUserId(userid);
        studioEntity.setUpdateTime(new Date());
        studioEntity.setBizId(id.getId());
        studioEntity.setType(ApplyType.PLAN);
        studioRespository.save(studioEntity);
    }

    @Override
    public StudioListVo getList(Pageable pageable, ApplyType type) {
        StudioListVo studioListVo = new StudioListVo();
        Page<StudioEntity> studioEntities;

        studioEntities = studioRespository.findAllByTypeOrderByCreateTimeDesc(type,pageable);

        studioListVo.setTotalCount(studioEntities.getTotalElements());

        List<StudioVo> list = new ArrayList<>();

        for(StudioEntity studioEntity :studioEntities.getContent()) {
            StudioVo studioVo = new StudioVo();
            studioVo.setUpdateTime(studioEntity.getUpdateTime());
            studioVo.setId(studioEntity.getBizId());
            studioVo.setCreateTime(studioEntity.getCreateTime());

            if (type == ApplyType.PLAN) {
                InformationEntity informationEntity = informationRepository.findOne(studioEntity.getBizId());
                if (informationEntity != null) {
                    studioVo.setDetailName(informationEntity.getTitle());
                    studioVo.setDetailId(informationEntity.getDetailId());
                }
            } else {
                ArtExamEntity artExamEntity = artExamRespository.findOne(studioEntity.getBizId());
                if (artExamEntity != null) {
                    studioVo.setDetailName(artExamEntity.getTitle());
                    studioVo.setDetailId(artExamEntity.getDetailId());
                }
            }


            UserEntity userEntity = userRepository.findOne(studioEntity.getUserId());
            if (userEntity != null) {
                studioVo.setAccount(userEntity.getAccount());
                studioVo.setName(userEntity.getName());
                studioVo.setUserId(userEntity.getId());
                studioVo.setNickname(userEntity.getNickname());
                studioVo.setMobile(userEntity.getMobile());
            }

            list.add(studioVo);
        }

        studioListVo.setList(list);

        return studioListVo;
    }

    @Override
    public StudioListVo getOwnList(String id, Pageable pageable, ApplyType type) {
        StudioListVo studioListVo = new StudioListVo();
        Page<StudioEntity> studioEntities;

        studioEntities = studioRespository.findBybizIdOrderByCreateTimeDesc(id,pageable);

        studioListVo.setTotalCount(studioEntities.getTotalElements());

        List<StudioVo> list = new ArrayList<>();

        for(StudioEntity studioEntity :studioEntities.getContent()) {
            StudioVo studioVo = new StudioVo();
            studioVo.setUpdateTime(studioEntity.getUpdateTime());
            studioVo.setId(studioEntity.getBizId());
            studioVo.setCreateTime(studioEntity.getCreateTime());

            if (type == ApplyType.PLAN) {
                InformationEntity informationEntity = informationRepository.findOne(studioEntity.getBizId());
                if (informationEntity != null) {
                    studioVo.setDetailName(informationEntity.getTitle());
                    studioVo.setDetailId(informationEntity.getDetailId());
                }
            } else {
                ArtExamEntity artExamEntity = artExamRespository.findOne(studioEntity.getBizId());
                if (artExamEntity != null) {
                    studioVo.setDetailName(artExamEntity.getTitle());
                    studioVo.setDetailId(artExamEntity.getDetailId());
                }
            }


            UserEntity userEntity = userRepository.findOne(studioEntity.getUserId());
            if (userEntity != null) {
                studioVo.setAccount(userEntity.getAccount());
                studioVo.setName(userEntity.getName());
                studioVo.setUserId(userEntity.getId());
                studioVo.setNickname(userEntity.getNickname());
                studioVo.setMobile(userEntity.getMobile());
            }

            list.add(studioVo);
        }

        studioListVo.setList(list);

        return studioListVo;
    }

    @Override
    public StudioIsIn getIsApply(String id, String studioId) {

        StudioIsIn studioIsIn = new StudioIsIn();

        List<StudioEntity> studioEntities = studioRespository.findByUserIdAndBizId(id,studioId);

        if (studioEntities.size() > 0) {
            studioIsIn.setIsApply(true);
        } else {
            studioIsIn.setIsApply(false);
        }

        return  studioIsIn;
    }

    @Override
    public long getCount(String id) {

        return studioRespository.countByBizId(id);
    }
}
